<?php
    error_reporting( E_ALL );                                                   // DEBUG: alle Fehler anzeigen
    ini_set( "display_errors", 1 );

    if( isset( $_REQUEST['system'] ) && isset( $_REQUEST['unit'] ) && isset( $_REQUEST['command'] ) )
        rcSwitch( $_REQUEST['system'], $_REQUEST['unit'], $_REQUEST['command'] );
    $ipAdr = "192.168.2.2";
	$path = "php";
//    <input type="button" value="Zur&uuml;ck" onClick="window.location='http://127.0.0.1'">
    echo "<form>\n";
    echo "  <table>\n";
    echo "    <tr>\n";
    echo "      <td>Esstisch</td>\n";
    echo "      <td><input type=\"button\" value=\"AN\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=1&command=1'\"></td>\n";
    echo "      <td><input type=\"button\" value=\"AUS\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=1&command=0'\"></td>\n";
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "      <td>Wandlampe</td>\n";
    echo "      <td><input type=\"button\" value=\"AN\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=3&command=1'\"></td>\n";
    echo "      <td><input type=\"button\" value=\"AUS\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=3&command=0'\"></td>\n";
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "      <td>Wohnzimmerfenster</td>\n";
    echo "      <td><input type=\"button\" value=\"AN\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=01111&unit=1&command=1'\"></td>\n";
    echo "      <td><input type=\"button\" value=\"AUS\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=01111&unit=1&command=0'\"></td>\n";
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "      <td>Flur</td>\n";
    echo "      <td><input type=\"button\" value=\"AN\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=2&command=1'\"></td>\n";
    echo "      <td><input type=\"button\" value=\"AUS\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=2&command=0'\"></td>\n";
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "      <td>Stehlampe</td>\n";
    echo "      <td><input type=\"button\" value=\"AN\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=01111&unit=2&command=1'\"></td>\n";
    echo "      <td><input type=\"button\" value=\"AUS\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=01111&unit=2&command=0'\"></td>\n";
    echo "    </tr>\n";
    echo "    <tr>\n";
    echo "      <td>Wandschrank</td>\n";
    echo "      <td><input type=\"button\" value=\"AN\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=4&command=1'\"></td>\n";
    echo "      <td><input type=\"button\" value=\"AUS\" onClick=\"window.location='http://$ipAdr/$path/index.php?system=11111&unit=4&command=0'\"></td>\n";
    echo "    </tr>\n";
    echo "  </table>\n";
    echo "</form>\n";


    
function rcSwitch( $sSystemCode, $sUnit, $sCommand )
{
    $bError = false;
/// @todo $sSystemCode, $sUnit, $sCommand auf Länge/Ziffern testen
    if( 5 != strlen( $sSystemCode ) ) return false;
    if( 1 != strlen( $sUnit ) ) return false;
    if( 1 != strlen( $sCommand ) ) return false;
	echo "sudo rc-send $sSystemCode $sUnit $sCommand<br>";
    $sResult = exec( "sudo rc-send $sSystemCode $sUnit $sCommand" );
    echo $sResult."<br>";
	if( 0 === strpos( $sResult, "sending systemCode[$sSystemCode] unitCode[$sUnit] command[$sCommand]" ) ) 
    {
        $bError = true;
/// @todo Wenn Befehl gesendet wurde, Status in DB aktuallisieren
    }
    return $bError;
}
?>
